package bancosys.tec.rtm.impl.diagnostician;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import bancosys.tec.rtm.diagnostician.AbstractDiagnostician;
import bancosys.tec.rtm.diagnostician.DiagnosisBean;

/**
 * Diagnosticador que lista todas as system properties do sistema e também tem a opção de verificar se as properties de um lista estão
 * configuradas ou não.
 * 
 * @author Evaristo
 * @version $Id:$ <b>Created on: </b> 02/02/2007
 */
public class SystemPropertyListDiagnostician extends AbstractDiagnostician {

    private final List<String> mandatoryProperties;

    /**
     * Construtor default para quando somente é necessário listar as properties do sistema.
     */
    public SystemPropertyListDiagnostician() {
        this.mandatoryProperties = new ArrayList<String>();
    }

    /**
     * Construtor para listar as properties do sistema e verificar se as properties da lista recebida no parâmetro estão configuradas no
     * sistema.
     * 
     * @param propertiesNeededList List
     */
    public SystemPropertyListDiagnostician(List<String> propertiesNeededList) {
        this.mandatoryProperties = propertiesNeededList;
    }

    /**
     * {@inheritDoc}
     */
    public String getName() {
        return "Propriedades do Sistema";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean fillDiagnosisBean(DiagnosisBean bean) throws Exception {
        Properties properties = System.getProperties();
        Iterator<Object> propertiesKeysIterator = properties.keySet().iterator();
        while (propertiesKeysIterator.hasNext()) {
            String propertyKey = propertiesKeysIterator.next().toString();
            String propertyValue = System.getProperty(propertyKey);
            bean.addDescription("Property: " + propertyKey + " || Valor: " + propertyValue);
        }

        if (this.mandatoryProperties.size() > 0) {
            Iterator<String> propertiesNeededIterator = this.mandatoryProperties.iterator();
            while (propertiesNeededIterator.hasNext()) {
                String property = propertiesNeededIterator.next();
                if (!properties.keySet().contains(property)) {
                    bean.addDescription("ERRO, PROPERTY \"" + property + "\" NÃO CONFIGURADA !!!");
                }
            }
        }
        return true;
    }
}
